package com.base.system.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.system.base.http.HttpResult;
import com.base.system.base.http.socket.WebSocket;
import com.base.system.mybatisPlus.entity.MybatisRole;
import com.base.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色区")
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private WebSocket socket;

    @PostMapping("/page")
    @ApiOperation("列表")
    @PreAuthorize("hasAuthority('role_list')")
    HttpResult<IPage<MybatisRole>> page(Page<MybatisRole> page, @RequestBody MybatisRole role) {
        return HttpResult.ok(roleService.page(page, role));
    }

    @PostMapping("/save")
    @ApiOperation("新增")
    @PreAuthorize("hasAuthority('role_add')")
    HttpResult<Void> save(@RequestBody MybatisRole role) {
        roleService.save(role);
        return HttpResult.ok();
    }

    @PutMapping("/update")
    @ApiOperation("修改")
    @PreAuthorize("hasAuthority('role_update')")
    HttpResult<Void> update(@RequestBody MybatisRole role) {
        roleService.update(role);
        return HttpResult.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    @PreAuthorize("hasAuthority('role_delete')")
    HttpResult<Void> delete(@PathVariable Long id) {
        roleService.delete(id);
        return HttpResult.ok();
    }

    @PostMapping("/addPer/{roleId}")
    @ApiOperation("添加权限关联")
    HttpResult<Void> addPer(@PathVariable Long roleId, @RequestBody List<Long> perIds) {
        roleService.addPer(roleId, perIds);
        return HttpResult.ok();
    }

    @GetMapping("/currentRoleWithPres/{roleId}")
    @ApiOperation("获取当前角色下的权限id集合")
    HttpResult<List<Long>> currentRoleWithPres(@PathVariable Long roleId) {
        return HttpResult.ok(roleService.currentRoleWithPres(roleId));
    }

    @GetMapping("/_all")
    @ApiOperation("获取所有角色信息")
    HttpResult<List<MybatisRole>> all() {
        return HttpResult.ok(roleService.all());
    }
}
